##############################################################################
# File-Name: compliance_for_analysis.r
# author: Tiago Ventura
# Purpose: Analysis compliance among treated participants. 
# Machine: macOS Ventura 13.5.1
# R version 4.3.1 
##############################################################################

# Packages ----------------------------------------------------------------
pacman::p_load(tidyverse, scales, lubridate, here, janitor, 
               rebus, RColorBrewer, wesanderson, ggridges)

# Source Auxiliary Code ---------------------------------------------------
source(here("code","_utils.r"))

# Figure 2 ---------------------------------------------------------------

# Data -----
d <- readRDS(here("data","all_processed_data.rds"))


## Recode variables ----
d$complaince_I_pos <- ifelse(d$compliance_I > -.1, d$compliance_I, NA)
d$q_whatsapp_num <- as.numeric(d$q_whatsapp)
d$compliance_self_reported_num <- as.numeric(fct_rev(d$compliance_self_reported))

## Models ------

# Measuring first stage effects on three different outcomes. 
## See models at _utils.r
list_false_items <- c("complaince_I_pos", "compliance_self_reported_num", "q_whatsapp_num")
list_itt <- map(list_false_items, ~ func_ols_base(.x, data=d))
list_ittc <- map(list_false_items, ~ func_ols_covariates(.x, data=d))
list_cace <- map(list_false_items, ~ func_cace(.x, data=d))

# plot all together
output = flatten(list(list_itt, list_ittc, list_cace))
models= c(rep("ITT", 3), rep("Cov-ITT", 3), rep("CACE", 3))
dep_var_label= rep(c("Behavioral Change \n Multimedia Consumption", "Self-Reported \n Multimedia Consumption", "Self-Reported \n WhatsApp daily usage"), 3)

# plots
plot_model(output,
           models, 
           dep_var_label, 
           title="", 
           caption= "Standardized First-Stage Treatment Effects on Compliance Measures")

save_function("fg2.tiff")
save_function("fg2.png")

# Fig 4 SM -------------------------------------------------------------
comp<- read_csv(here("data", "compliance.csv"))



# Compliance variation between treatment and control -------------------------------------------------------------------------
## colors
treat <- wesanderson::wes_palette("BottleRocket2")[[2]]
control <- wesanderson::wes_palette("BottleRocket2")[[3]]

# pre-process
comp <- comp %>%
          # removing negative variations that occur due to users resetting their statistics or some unpredictable updates on Whatsapp
          filter(compliance_I>-.1) %>% 
          select(exp, value=compliance_I) %>%
          mutate(exp=str_to_title(exp), 
                 # log values
                 value=log(value+1)) %>%
          group_by(exp) %>%
          # get the median by groups
          mutate(median=median(value))

# plot
ggplot(comp, aes(x= value, y=exp,fill=exp, color=exp)) +
  stat_density_ridges(quantile_lines = TRUE, quantiles = 4, 
                      alpha=.4) +
  scale_fill_manual(values=c(treat, control),
                    name="",
                    guide = guide_legend(reverse=TRUE, title.position="top")) +
  scale_color_manual(values=c(treat, control),
                    name="",
                    guide = guide_legend(reverse=TRUE, title.position="top")) +
  
  labs() +
  ylab("") +
  xlab("Change in multimedia consumption (log scale)") +
  labs(title="", 
       subtitle="", 
       caption="Lines represent the first, the median, and third terciles for the distributions") 

# save
ggsave(filename = here("output", "sm_fig4.png"), 
       width = 12, height = 8, units = "in", 
       pointsize = 12, bg = "white")
ggsave(filename = here("output", "sm_fig4.tiff"), 
       width = 12, height = 8, units = "in", 
       pointsize = 12, bg = "white")



